package com.ruoyi.quartz.task;

import com.ruoyi.integration.config.WeatherServiceProperties;
import com.ruoyi.integration.response.WeatherResponse;
import com.ruoyi.integration.service.impl.WeatherStationDataServiceImpl;
import com.ruoyi.iot.dto.Weather;
import com.ruoyi.iot.mapper.WeatherMapper;
import com.ruoyi.iot.utils.IoTConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Component("weatherDataUpdateTask")
@RequiredArgsConstructor
public class WeatherDataUpdateTask {

    private final WeatherMapper weatherMapper;

    private final WeatherStationDataServiceImpl weatherStationDataService;

    private final WeatherServiceProperties weatherServiceProperties;

    public void updateWeatherData() {

        weatherServiceProperties.getDeviceIds().forEach(deviceId -> {
            WeatherResponse weatherResponse = null;
            try {
                weatherResponse = weatherStationDataService.fetchData(deviceId);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
            List<Weather> weatherList = IoTConverter.weatherDataConvert(weatherResponse);
            weatherMapper.batchInsert(weatherList);
        });
    }
}
